package com.basics.framework.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.basics.common.constant.Constants;
import com.basics.framework.constants.WxProperties;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 */
@Configuration
@Slf4j
public class YzWxMaConfiguration {

    @Autowired
    private WxProperties wxProperties;

    @Bean
    public WxMaService wxMaService() {
        log.error("微信参数:{}",wxProperties);
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(wxProperties.getAppid());
        config.setSecret(wxProperties.getSecret());
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);
        return wxMaService;
    }
    @Bean
    @ConditionalOnMissingBean
    public WxPayService wxService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(wxProperties.getAppid());
        payConfig.setMchId(wxProperties.getMchId());
        payConfig.setMchKey(wxProperties.getMchKey());
        payConfig.setKeyPath(wxProperties.getPrivateCertPath());
        payConfig.setTradeType(wxProperties.getTradeType());
        payConfig.setNotifyUrl(wxProperties.getNotifyUrl());
        payConfig.setApiV3Key(wxProperties.getApiKey());
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

}
